ESP8266 I2C 使用说明 |
您所在的位置:网站首页 › esp8266 上拉电阻 › ESP8266 I2C 使用说明 |
1. 概述 ESP8266 作为 I2C 主机的 SDA 与 SCL 线波形由 GPIO 模拟产生,在 SCL 的上升沿之后 SDA 取数。 SCL 高低电平各保持 5us, 因此 I2C 时钟频率约为 100KHz。 2. I2C Master 接口 2.1 初始化 i2c_master_gpio_init: GPIO 硬件初始化 具体如下: (1) 选择pin脚功能,配置为GPIO (2) 配置GPIO为开漏模式 (3) 初始化SDA与SCL为高电平 (4) 使能GPIO中断并复位从机状态 i2c_master_init(void): 复位从机状态2.2 I2C起始 2.3 I2C停止 2.4 I2C主机回复ACK 2.5 I2C主机回复NACK 2.6 检查I2C从机应答 2.7 向I2C总线写数据 2.8 从I2C总线读数据 3. 使用示例 void ICACHE_FLASH_ATTR user_mvh3004_init(void) { i2c_master_gpio_init(); } LOCAL bool ICAHE_FLASH_ATTR user_mvh3004_burst_read(uint8 addr, uint8 *pData, uint16 len) { uint8 ack; uint16 i; i2c_master_start(); i2c_master_writeByte(addr); ack = i2c_master_checkAck(); if (!ack) { os_printf("addr not ack when tx write cmd \n"); i2c_master_stop(); return false; } i2c_master_stop(); i2c_master_wait(40000); i2c_master_start(); i2c_master_writeByte(addr + 1); ack = i2c_master_checkAck(); if (!ack) { os_printf("addr not ack when tx write cmd \n"); i2c_master_stop(); return false; } for (i = 0; i < len; i++) { pData[i] = i2c_master_readByte(); if (i == (len - 1)) i2c_master_send_nack(); else i2c_master_send_ack(); } i2c_master_stop(); return true; }
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |